package kiwi.core.webservices.search;

import kiwi.core.api.config.ConfigurationService;
import kiwi.core.api.search.SolrIndexingService;
import kiwi.core.qualifiers.transaction.Transactional;
import kiwi.core.services.search.SolrIndexingServiceImpl;
import kiwi.core.services.search.solr.program.parser.src.Shortcuts;
import org.slf4j.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Thomas Kurz
 * Date: 25.03.11
 * Time: 15:58
 */
@Path("/solrconfig")
@Transactional
@ApplicationScoped
public class SolrSearchConfigurationWebservice {

	@Inject
	Logger log;

	private Shortcuts parser = null;

	@Inject
    private ConfigurationService configurationService;

	@Inject
	private SolrIndexingService solrIndexingService;

	@Path("/program")
	@POST
	public Response loadProgram(@QueryParam("reindex") boolean reindex, @Context HttpServletRequest context) {
		try {
			solrIndexingService.parse(context.getInputStream());
			if(reindex) {
				log.info("rebuild solr index");
				solrIndexingService.rebuildIndex();
			}
			return Response.ok().build();
		} catch (Exception e) {
			return Response.serverError().entity(e.getStackTrace().toString()).build();
		}
	}

	@Path("/program")
	@GET
	@Produces("application/json")
	public Map<String,Object> getProgram(){
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("value", SolrIndexingServiceImpl.ProgramString());
		return m;
	}

}
